/*
	This is for the dumb switching circuit
	It is instructed via RF wireless to switch up to 4 relays
	Watches for external changes on D0-D3
	Grounds C0-C3 to turn on
*/



#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))

int main(void)
{
	CPU_PRESCALE(0);	// ?

	// Set Inputs
	EIMSK = 0x0;
	
	// Set interrupt on any state change
	EICRA = (1<<ISC00);
	EICRA |= (1<<ISC10);
	EICRA |= (1<<ISC20);
	
	// Set D0 - D2 to interrupt pins
	EIMSK = (1<<INT0);
	EIMSK |= (1<<INT1);
	EIMSK |= (1<<INT2);
	
	// Set Outputs
	DDRD |= (1<<4);
	DDRD |= (1<<5);
	DDRD |= (1<<6);
	
	// Set Input to pull up (will be 1 if not grounded)
	PORTD |= (1<<0);
	PORTD |= (1<<1);
	PORTD |= (1<<2);

	sei();
	
	_delay_ms(300);
	
	while(1)
	{
	/*
		for (i=0; i<4; i++)
		{
			// Cycle through outputs
			PORTC &= ~(1<<i);
			_delay_ms(300);
			PORTC |= (1<<i);
		}
	*/
	}
}

ISR (INT0_vect)
{
	if (PIND & (1<<0))
	{
		PORTD |= (1<<4);
	}
	else
	{
		PORTD &= ~(1<<4);
	}
}

ISR (INT1_vect)
{
	if (PIND & (1<<1))
	{
		PORTD |= (1<<5);
	}
	else
	{
		PORTD &= ~(1<<5);
	}
}

ISR (INT2_vect)
{
	if (PIND & (1<<2))
	{
		PORTD |= (1<<6);
	}
	else
	{
		PORTD &= ~(1<<6);
	}
}